const WXAPI = require('apifm-wxapi')
const AUTH = require('../../utils/auth')

const app = getApp()
Page({
  data: {
    addressList: []
  },

  selectTap: function(e) {
    var id = e.currentTarget.dataset.id;
    const selectAdd = this.data.addressList.filter(item => item.id === id)[0]

    // WXAPI.updateAddress({...selectAdd, ifDefault: 'Y'}).then(function(res) {
    //   wx.navigateBack({})
    // })
  },

  addAddess: function() {
    wx.navigateTo({
      url: "/pages/address-add/index"
    })
  },

  editAddess: function(e) {
    wx.navigateTo({
      url: "/pages/address-add/index?id=" + e.currentTarget.dataset.id
    })
  },

  onLoad: function() {
    // 屏蔽分享
    wx.hideShareMenu();
  },
  onShow: function() {
    // wx.getStorage({
    //   key: '',
    //   success: function(res) {},
    // })
    AUTH.checkHasLogined().then(isLogined => {
      if (isLogined) {
        this.initShippingAddress();
      } else {
        wx.showModal({
          title: '提示',
          content: '本次操作需要您的登录授权',
          cancelText: '暂不登录',
          confirmText: '前往登录',
          success(res) {
            if (res.confirm) {
              wx.switchTab({
                url: "/pages/my/index?page=selectAddress"
              })
            } else {
              wx.navigateBack()
            }
          }
        })
      }
    })
  },
  initShippingAddress: function() {
    var that = this;
    WXAPI.queryAddress().then(function(res) {
      if (res.retCode == "200") {
        that.setData({
          addressList: res.data
        });
      } 
    })
  }

})